<?php

/* EXAMPLE OF USAGE

$this->fatal_error('A big crappy error');
$this->dump_error('Lalala error 1');
$this->dump_warning('Lalala warning 1');
$this->dump_error('Lalala error 2');

*/

class ErrorManager{
	private $fatal_error;
	private $queue;
	private $template_dir;
	private $output;
	private $additional_output;
	
	public $manager_error;
	
	public function ErrorManager($template_dir){
		$this->queue = array();
		$this->backtrace = null;
		$this->template_dir = $template_dir;
	}
	
	public function fatal_error($text){
		$this->queue[] = array('type'=>'fatal_error', 'text'=>$text);
		$this->flush();
		die();
	}
	
	public function add($text){
		$this->additional_output[] = $text;
	}
	
	public function dump_error($text){
		if(DEBUG) $this->queue[] = array('type'=>'error', 'text'=>$text);
	}
	
	public function dump_warning($text){
		if(DEBUG) $this->queue[] = array('type'=>'warning', 'text'=>$text);
	}
	
	public function debug($text, $title=null){
		if(DEBUG){
			if($title){
				$text = '<div style="background-color:#333;color:#fff;padding:2px 5px;margin-bottom:5px;">'.$title.'</div>'.$text;
			}
			$this->queue[] = array('type'=>'debug', 'text'=>$text);
		}
	}
	
	public function flush(){
		if(count($this->queue) > 0){
			foreach($this->queue as $q){
				$this->_parse_template($q['type'], $q['text'], $q['type']=='fatal_error' ? true : false);
			}
			$this->output();
			$this->queue = array();
		}
	}
	
	private function _parse_template($template, $error_text, $fatal = false){
		$template = ERR_TPL_PATH.$template.'.html';
		if(!file_exists($template)){
			$this->manager_error = 'Template file <strong>'.$template.'</strong> does not exist';
			return false;
		}
		$html = file_get_contents($template);
		
		//parse
		$html = str_replace('{$error}', $error_text, $html);
		$html = str_replace('{$url}', ERR_TPL_DIR, $html);
		
		//backtrace
		if($fatal){
			$backtrace = debug_backtrace();
			$backtrace_html = '';
			for($i = 1;$i<count($backtrace);$i++){
				$b = $backtrace[$i];
				$cl = $i%2==0 ? 'even' : 'odd';
				$backtrace_html.= '<li class="'.$cl.'"><div class="left">File:</div><div class="right">'.$b['file'].' (line '.$b['line'].')</div><br/>';
				if(isset($b['function'])){
					$backtrace_html.= '<div class="left">Function:</div><div class="right">'.$b['function'].'</div><br/>';
				}
				if(isset($b['class'])){
					$backtrace_html.= '<div class="left">Class:</div><div class="right">'.$b['class'].'</div><br/>';
				}
				$backtrace_html.= '</li>';
			}
			$html = str_replace('{$backtrace}', $backtrace_html, $html);
		}
		$this->output[] = $html;
	}
	
	public function output($return=false){
		$html = implode('',$this->output);
		if($this->additional_output){
			$html.= implode('',$this->additional_output);
		}
		if($return)
			return $html;
		else
			echo $html;
	}
	
	public function get_fatal_error(){
		return $this->fatal_error();
	}
	
	public function get_errors(){
		return $this->errors;
	}
	
	public function get_warnings(){
		return $this->warnings;
	}
}
?>